package com.sikaryofficial.backend.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.sikaryofficial.backend.domain.dto.req.AddressAreaReq;
import com.sikaryofficial.backend.domain.dto.req.AddressCityReq;
import com.sikaryofficial.backend.domain.dto.req.AddressListReq;
import com.sikaryofficial.backend.service.address.AddressCacheService;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.core.utils.HiResultUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.HiResult;
import com.sikaryofficial.system.api.model.AddressDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 地址Controller
 *
 * @author qinjinyuan
 * @date 2023-11-14
 */
@RestController
@RequestMapping("/address")
@Api(tags = "地址服务")
public class AddressController extends BaseController {
    @Value("${hismk.backend.defaultCountry}")
    private String defaultCountry;

    @Autowired
    private AddressCacheService addressCacheService;

    /**
     * 查询国家列表
     */
    @PostMapping("/country")
    @ApiOperation("查询国家列表")
    public HiResult<List<String>> countryList() {
        return HiResultUtil.success(addressCacheService.countryList());
    }

    /**
     * 查询区域列表
     */
    @PostMapping("/area")
    @ApiOperation("查询区域列表【法律法规Province/State】")
    public HiResult<List<String>> areaList(@RequestBody(required = false) AddressAreaReq req) {
        return HiResultUtil.success(addressCacheService.areaList(req));
    }

    /**
     * 查询城市列表
     */
    @PostMapping("/city")
    @ApiOperation("查询城市列表")
    public HiResult<List<String>> cityList(@RequestBody AddressCityReq req) {
        if (CharSequenceUtil.isBlank(req.getArea())) {
            throw new ServiceException("please select the area.");
        }
        return HiResultUtil.success(addressCacheService.cityList(req));
    }

    /**
     * 查询城市列表
     */
    @PostMapping("/cityMap")
    @ApiOperation("查询城市列表")
    public HiResult<Map<String,Long>> cityMap(@RequestBody AddressCityReq req) {
        if (CharSequenceUtil.isBlank(req.getArea())) {
            throw new ServiceException("please select the area.");
        }
        return HiResultUtil.success(addressCacheService.cityMap(req));
    }

    /**
     * 查询地址列表
     */
    @PostMapping("/list")
    @ApiOperation("查询地址列表")
    public HiResult<AddressDTO> list(@Validated @RequestBody AddressListReq req) {
        if (CharSequenceUtil.isBlank(req.getArea()) || CharSequenceUtil.isBlank(req.getCity())) {
            throw new ServiceException("please select the area and city.");
        }
        return HiResultUtil.success(addressCacheService.getAddressListByParam(req));
    }

}
